﻿namespace LeetCodeProject._3001_4000._3239;

// 最少翻转次数使二进制矩阵回文 I
public class Solution
{
    public int MinFlipsGF(int[][] grid)
    {
        int rowCnt = 0, colCnt = 0;
        int m = grid.Length, n = grid[0].Length;
        for (int i = 0; i < m; i++)
        {
            for (int j1 = 0, j2 = n - 1; j1 < j2; j1++, j2--)
            {
                if ((grid[i][j1] ^ grid[i][j2]) != 0)
                {
                    rowCnt++;
                }
            }
        }

        for (int j = 0; j < n; j++)
        {
            for (int i1 = 0, i2 = m - 1; i1 < i2; i1++, i2--)
            {
                if ((grid[i1][j] ^ grid[i2][j]) != 0)
                {
                    colCnt++;
                }
            }
        }

        return Math.Min(colCnt, rowCnt);
    }


    public int MinFlips(int[][] grid)
    {
        int m = grid.Length, n = grid[0].Length;
        int rowFlips = 0;
        for (int i = 0; i < m; i++)
        {
            for (int j1 = 0, j2 = n - 1; j1 < j2; j1++, j2--)
            {
                if (grid[i][j1] != grid[i][j2])
                {
                    rowFlips++;
                }
            }
        }

        int colFlips = 0;
        for (int j = 0; j < n; j++)
        {
            for (int i1 = 0, i2 = m - 1; i1 < i2; i1++, i2--)
            {
                if (grid[i1][j] != grid[i2][j])
                {
                    colFlips++;
                }
            }
        }

        return Math.Min(rowFlips, colFlips);
    }
}